<template>
  <div class="create-cost-dialog">
    <el-dialog
      :visible.sync="visible"
      :append-to-body="false"
      :before-close="handleCancel"
      class="dialog-size-auto small-common-dialog-size"
      width="450px">
      <div class="main">
        <Title title="采集链接" font-size="16px" style="margin-bottom:20px;"/>
        <el-form
          ref="projectForm"
          :model="form"
          label-position="right"
          label-width="90px"
          class="lineHeight32">
          <el-form-item label="采集链接" prop="projectName">
            <upAttachment
              ref="uploadImg"
              :import-api="'/common/file/upload'"
              :hide-confirm="true"
              :is-disabled="false"
              :file-list="form.collectUrl"
              :limit="1"
              btn-text="上传附件"
              @upProgress="upProgress"
              @complateSetData="complateSetData"
              @removeFileList="removeFileList"
            />
          </el-form-item>
        </el-form>
        <div class="btn-container">
          <el-button :loading="btnLoading" size="small" type="primary" @click="handleConfirm">确定</el-button>
          <el-button type="info" class="cancel-btn-style" size="small" @click="handleCancel">取消</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Title from '@/components/Title';
import upAttachment from '@/components/Common/upAttachment/index';
import { editReportDetail } from '@/api/publish';
import ElMessage from '@/utils/message';

export default {
  components: { Title, upAttachment },
  data() {
    return {
      visible: false,
      id: '',
      form: {
        collectUrl: []
      },
      btnLoading: false
    };
  },
  methods: {
    initDialog(obj) {
      this.id = obj.id;
      this.visible = true;
    },
    upProgress() {
      this.createLoading = true;
    },
    complateSetData({ data, fileName }) {
      this.createLoading = false;
      const fileList = {
        name: fileName,
        url: data.url,
        time: new Date().getTime()
      };
      if (typeof data === 'string') {
        fileList.url = data;
      } else {
        fileList.url = data.url;
      }
      !this.form.collectUrl && (this.form.collectUrl = []);
      this.form.collectUrl.push(fileList);
    },
    removeFileList({ file, index }) {
      this.form.collectUrl.splice(index, 1);
    },
    handleConfirm() {
      if (this.form.collectUrl.length > 0) {
        this.btnLoading = true;
        const url = this.form.collectUrl[0].url;
        editReportDetail(this.id, { collectUrl: url }).then(res => {
          this.btnLoading = false;
          this.$message.success('添加成功');
          this.$emit('updateData');
          this.handleCancel();
        }).catch(err => {
          this.btnLoading = false;
          console.log(err);
        });
      } else {
        ElMessage.warning('请上传链接附件');
      }
    },
    handleCancel() {
      this.$refs['projectForm'].resetFields();
      this.form.collectUrl = [];
      this.visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.width240 {
  width: 240px;
}
.btn-container {
  display: flex;
  justify-content: center;
}
</style>
